<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Gmap
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Gmap_Form_Create extends Engine_Form
{
  public $_error = array();

  public function init()
  {
    $this->setTitle('Post New Gmap')
      ->setDescription('Please fill out the form below, then click "Post Gmap" to submit your gmap.')
      ->setAttrib('name', 'gmaps_create')
      ;

    $user = Engine_Api::_()->user()->getViewer();
    $user_level = Engine_Api::_()->user()->getViewer()->level_id;
    
    
    $this->addElement('Text', 'title', array(
      'label' => 'Title',
      'description' => 'Enter name of this location. Example: Sweet Home',
      'allowEmpty' => false,
      'required' => true,
      'validators' => array(
        array('NotEmpty', true),
        array('StringLength', false, array(1, 64)),
      ),
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
      ),
    ));
    $this->title->getDecorator("Description")->setOption("placement", "append");
            
    $this->addElement('Text', 'location', array(
      'label' => 'Location Address',
      'description' => 'Example: 123 S Main St, Los Angeles, CA 90012',
      'allowEmpty' => false,
      'required' => true,
      'validators' => array(
        array('NotEmpty', true),
        array('StringLength', false, array(1, 255)),
        new Radcodes_Lib_Validate_Location_Address(),
      ),
      'filters' => array(
        'StripTags'
      ),
    ));       
    $this->location->getDecorator("Description")->setOption("placement", "append");
    
    $this->addElement('Textarea', 'description', array(
      'label' => 'Description',
      'description' => 'Maximum: 500 characters',
      'validators' => array(
        array('NotEmpty', true),
        array('StringLength', false, array(1, 500)),
      ),
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_HtmlSpecialChars(),
        new Engine_Filter_EnableLinks(),
      ),
    ));  
    $this->description->getDecorator("Description")->setOption("placement", "append");
    
    $allowed_upload = Engine_Api::_()->authorization()->getPermission($user_level, 'gmap', 'photo');
    //$is_mobile = preg_match("/iphone|ipod|ipad|android|webos/i", strtolower($_SERVER['HTTP_USER_AGENT']));
    if($allowed_upload){
      $this->addElement('File', 'photo', array(
        'label' => 'Main Photo'
      ));
      $this->photo->addValidator('Extension', false, 'jpg,png,gif');
    }
    
    // prepare categories
    $categories = Engine_Api::_()->gmap()->getCategories();
    if (count($categories)!=0){
      $categories_prepared = Engine_Api::_()->gmap()->convertCategoriesToArray($categories);
      $categories_prepared = array(""=>"") + $categories_prepared;
      
      // category field
      $this->addElement('Select', 'category_id', array(
            'label' => 'Category',
            'multiOptions' => $categories_prepared,
			      'allowEmpty' => false,
			      'required' => true,
			      'validators' => array(
			        array('NotEmpty', true),
			      ),
			      'filters' => array(
			       'Int'
			      ),
          ));
    }
    
    $this->addElement('Text', 'tags',array(
      'label'=>'Tags (Keywords)',
      'autocomplete' => 'off',
      'description' => 'Separate tags with commas.',
      'filters' => array(
        new Engine_Filter_Censor(),
      ),
    ));
    $this->tags->getDecorator("Description")->setOption("placement", "append");
    
    
    $this->addElement('Radio', 'home', array(
      'label' => 'User Home',
      'description' => 'Would you like to set this as your default location?',
      'multiOptions' => array(
        1 => 'Yes',
        0 => 'No',
      ),
      'value' => 'everyone',
    ));
    $this->home->getDecorator('Description')->setOption('placement', 'prepend');
    
    // Add subforms
    if (!$this->_item){
      $customFields = new Gmap_Form_Custom_Fields();
    }
    else $customFields = new Gmap_Form_Custom_Fields(array('item'=>$this->getItem()));
    
    $this->addSubForms(array(
      'customField' => $customFields
    ));
    
    // View
    $availableLabels = array(
      'everyone'              => 'Everyone',
      'registered'            => 'Registered Members',
      'owner_network'         => 'Friends and Networks',
      'owner_member_member'   => 'Friends of Friends',
      'owner_member'          => 'Friends Only',
      'owner'                 => 'Just Me'
    );

    $options = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('gmap', $user, 'auth_view');
    $options = array_intersect_key($availableLabels, array_flip($options));

    $this->addElement('Radio', 'auth_view', array(
      'label' => 'Privacy',
      'description' => 'Who may see this gmap?',
      'multiOptions' => $options,
      'value' => 'everyone',
    ));
    $this->auth_view->getDecorator('Description')->setOption('placement', 'prepend');

    $options =(array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('gmap', $user, 'auth_comment');
    $options = array_intersect_key($availableLabels, array_flip($options));

    // Comment
    $this->addElement('Radio', 'auth_comment', array(
      'label' => 'Comment Privacy',
      'description' => 'Who may post comments on this gmap?',
      'multiOptions' => $options,
      'value' => 'registered',
    ));
    $this->auth_comment->getDecorator('Description')->setOption('placement', 'prepend');

    $this->addElement('Checkbox', 'search', array(
      'label' => 'People can search for this gmap',
      'value' => True
    ));

    
    // Buttons
    $this->addElement('Button', 'submit', array(
      'label' => 'Post Gmap',
      'type' => 'submit',
      'ignore' => true,
      'decorators' => array('ViewHelper')
    ));

    $this->addElement('Cancel', 'cancel', array(
      'label' => 'cancel',
      'link' => true,
      'prependText' => ' or ',
      'decorators' => array(
        'ViewHelper'
      )
    ));
    
    $this->addDisplayGroup(array('submit', 'cancel'), 'buttons');
    $button_group = $this->getDisplayGroup('buttons');
    $button_group->addDecorator('DivDivDivWrapper');    

    
  }

}

